package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.User;
import com.example.wechatdaka.entity.UserTeacher;
import com.example.wechatdaka.entity.UserWechat;
import com.example.wechatdaka.mapper.UserMapper;
import com.example.wechatdaka.mapper.UserTeacherMapper;
import com.example.wechatdaka.service.UserTeacherService;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-27
 */
@Api(tags = "教师模块")
@RestController
@RequestMapping("/userteacher")
public class UserTeacherController {
    @Autowired
    private UserTeacherMapper userTeacherMapper;
    @Autowired
    private UserTeacherService userTeacherService;
    @ApiOperation(value = "选择教师")
    @RequestMapping(value = "/selectTeachers",method = RequestMethod.POST)
    public Result selectAddTeachers(){

        List<User> teacherList = userTeacherMapper.selectAddTeacher();
        return new Result(teacherList);
    }
    @ApiOperation(value = "添加教师信息")
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Result addTeacher(@RequestBody UserTeacher userTeacher){
        System.out.println(userTeacher.toString());
        boolean b = userTeacherService.saveOrUpdate(userTeacher);
        if (b){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,"","Failed");
    }

    @ApiOperation(value = "查询教师列表")
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result QueryAllTeachers(){
        List<UserTeacher> userTeachers = userTeacherMapper.queryAllTeacher();
        return new Result(userTeachers);
    }
    @ApiOperation(value = "分页查询教师列表")
    @RequestMapping(value = "/indexByPage",method = RequestMethod.GET)
    public JsonResult queryAllByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize) {
        Page<UserTeacher> page = new Page<>(currentPage, pagesize);
        IPage<UserTeacher> userWechatIPage = userTeacherMapper.queryAllTeacherPage(page);
        List<UserTeacher> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "根据id删除教师")
    @RequestMapping(value = "/deleteTeacher",method = RequestMethod.POST)
    public Result deleteTeacher(@RequestBody String id){
        System.out.println(id);
        int deleteById = userTeacherMapper.deleteById(id);
        if (deleteById>0){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"Failed");
    }
    @ApiOperation(value = "分页根据关键字查询教师列表")
    @RequestMapping(value = "/queryByKeyword",method = RequestMethod.GET)
    public JsonResult queryByKeyword(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String keyword) {
        Page<UserTeacher> page = new Page<>(currentPage, pagesize);
        IPage<UserTeacher> userWechatIPage = userTeacherMapper.queryByKeywordPage(page,keyword);
        List<UserTeacher> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "根据工号查询教师")
    @RequestMapping(value = "/queryByNumber",method = RequestMethod.POST)
    public Result queryByNumber(@RequestBody String id){
        UserTeacher userTeacher = userTeacherMapper.queryByNumber(id);
        return new Result(userTeacher);
    }

}

